use rand::{seq::SliceRandom, thread_rng};
use sgs_core::player::Identity;

/// 对 Vec<Identity> 的 shuffle 实现
pub trait HeadShuffle {
    /// 打乱所有数据
    fn shuffle_all(&self) -> Self;

    /// 保留第一项，剩下的打乱
    fn shuffle_keep_head(&self) -> Self;
}

impl HeadShuffle for Vec<Identity> {
    fn shuffle_all(&self) -> Self {
        let mut rng = thread_rng();
        let mut identity_list = self.clone();
        identity_list.shuffle(&mut rng);
        identity_list
    }

    fn shuffle_keep_head(&self) -> Self {
        let mut identity_list = self.clone();

        if let Some(_) = identity_list.first() {
            let mut rng = thread_rng();
            let (_, rest) = identity_list.split_at_mut(1);
            rest.shuffle(&mut rng);
        }

        identity_list
    }
}

#[cfg(test)]
mod test {

    use crate::{pkg::Pattern, utils::PatternOption};

    use super::*;

    #[test]
    fn test_shuffle_identity_list() {
        let pattern: Pattern = PatternOption::Player12.into();

        let idls = pattern.identity_list;

        let s_idls = idls.shuffle_all();

        let s_idls2 = idls.shuffle_keep_head();

        println!("{:?}", s_idls);

        println!("{:?}", s_idls2);

        assert_eq!(s_idls2[0], Identity::Lord);
    }
}
